<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <div class="base-content-operate">
        <el-button
          v-if="ExportButton"
          v-rights="'Export'"
          v-loading="uploadLoad"
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData"
          >导出</el-button
        >
      </div>
      <el-main>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane
            v-for="(item, index) in tablist"
            :key="index"
            :label="item.title"
            :name="item.name"
          >
            <EntrustOrderPriceWaitList
              v-if="index == 0 && isFirst"
              ref="EntrustOrderPriceWaitList"
              :data-list="finishdataList"
              @getdataList="getdataList"
              @updateQueryForm="updateQueryForm"
            />
            <EntrustOrderPriceFinishList
              v-if="index == 1 && isSecond"
              ref="EntrustOrderPriceFinishList"
              :data-list="finishdataList"
              @getdataList="getdataList"
              @updateQueryForm="updateQueryForm"
            />
          </el-tab-pane>
        </el-tabs>
      </el-main>

      <!--分页-->
      <BasePagination
        :total="total"
        :current-page="pageInfo.pageIndex"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>
  </div>
</template>
<script>
import { getList } from "../../../api/EntrustManage/EntrustOrderPrice.js";
import EntrustOrderPriceFinishList from "./EntrustOrderPriceFinishList.vue";
import EntrustOrderPriceWaitList from "./EntrustOrderPriceWaitList.vue";
import {
  ExportWord,
  DownloadExportWordZip,
} from "../../../api/EntrustManage/ExportWord.js";
export default {
  name: "EntrustOrderPriceList",
  components: {
    EntrustOrderPriceFinishList,
    EntrustOrderPriceWaitList,
  },
  data() {
    return {
      ExportButton: false,
      uploadLoad: false,
      loading: false,
      searchLoading: false,
      queryForm: {
        orderNum: "",
        customerName: "",
        pricelStatusCode: 0,
      },
      pageInfo: {
        pageIndex: 1,
        pageSize: 10,
      },
      total: 1,
      waitPageIndex: 1,
      finishPageIndex: 1,
      dialogFormVisible: false,
      tablist: [
        { title: "待办", name: "first" },
        { title: "已办", name: "second" },
      ],
      activeName: "first",
      isFirst: true,
      isSecond: false,
      dataList: [],
      finishdataList: [],
    };
  },
  created() {
    if (this.$route.query.orderNum !== undefined) {
      this.queryForm.orderNum = this.$route.query.orderNum;
      this.pageInfo.pageIndex = 1;
      this.queryForm.pricelStatusCodeSymbol = "";
      this.queryForm.pricelStatusCode = 1;
      this.queryForm.finishedState = "0";
      getList(this.queryForm, this.pageInfo).then((res) => {
        if (res.data.dataList !== null) {
          this.activeName = "first";
          this.ExportButton = false;
        } else {
          this.activeName = "second";
          this.ExportButton = true;
        }
        this.getdataList(true);
      });
    } else {
      this.queryForm.pricelStatusCode = 1;
      this.getdataList(true);
    }
  },
  methods: {
    exportData() {
      this.uploadLoad = true;
      var idList = [];
      if (this.activeName === "first") {
        for (
          let i = 0;
          i < this.$refs.EntrustOrderPriceWaitList[0].tableDatas.length;
          i++
        ) {
          idList.push(this.$refs.EntrustOrderPriceWaitList[0].tableDatas[i].id);
        }
      } else {
        for (
          let i = 0;
          i < this.$refs.EntrustOrderPriceFinishList[0].tableDatas.length;
          i++
        ) {
          idList.push(
            this.$refs.EntrustOrderPriceFinishList[0].tableDatas[i].id
          );
        }
      }

      ExportWord({ id: idList, exportWordTypeEnum: 2 })
        .then((res) => {
          DownloadExportWordZip({ path: res.data, exportWordTypeEnum: 2 })
            .then((res) => {
              this.uploadLoad = false;
              var blob = new Blob([res.data], {
                type: "application/octet-stream",
              });
              var url = window.URL.createObjectURL(blob);
              const link = document.createElement("a"); // 创建a标签
              link.href = url;
              link.download = "价格核算.zip"; // 重命名文件
              link.click();
              URL.revokeObjectURL(url); // 释放内存
            })
            .catch(() => {
              this.uploadLoad = false;
            });
        })
        .catch(() => {
          this.uploadLoad = false;
        });
    },
    handleClick(tab, event) {
      this.queryForm = this.$options.data().queryForm;

      if (tab.name === "first") {
        this.queryForm.pricelStatusCode = 1;
        this.isFirst = true;
        this.isSecond = false;
        this.ExportButton = false;
      } else if (tab.name === "second") {
        this.queryForm.pricelStatusCode = -1;
        this.queryForm.discountTypeCode = -1;
        this.isFirst = false;
        this.isSecond = true;
        this.ExportButton = "true";
      }
      this.getdataList();
    },
    getdataList(flag) {
      this.dataList = [];
      this.finishdataList = [];

      if (flag === "filter") {
        this.isSearch = true;
      }
      if (flag) {
        this.pageInfo.pageIndex = 1;
      }
      this.searchLoading = true;

      this.loading = true;
      if (this.activeName === "first") {
        this.queryForm.pricelStatusCodeSymbol = "";
        this.queryForm.finishedState = "0";
      } else {
        this.queryForm.pricelStatusCodeSymbol = ">";
        this.queryForm.finishedState = "1";
      }

      getList(this.queryForm, this.pageInfo)
        .then((res) => {
          this.loading = false;
          this.searchLoading = false;
          this.finishdataList = res.data.dataList ? res.data.dataList : [];
          this.total = res.data.total;
        })
        .catch(() => {
          this.loading = false;
          this.searchLoading = false;
        });
    },
    // 切换页码
    changePage(page) {
      this.pageInfo.pageIndex = page;
      this.getdataList(false);
    },
    sizeChange(val) {
      this.pageInfo.pageIndex = 1;
      this.pageInfo.pageSize = val;
      this.getdataList(false);
    },

    // 清空筛选条件
    cancelForm() {
      this.queryForm = this.$options.data().queryForm;
      this.getdataList("filter");
    },
    updateQueryForm(obj) {
      this.queryForm.orderNum = obj.orderNum;
      this.queryForm.customerName = obj.customerName;
      this.queryForm.sampleName = obj.sampleName;
      this.queryForm.discountTypeCode = obj.discountTypeCode;
      this.queryForm.pricelStatusCode = obj.pricelStatusCode;
      this.getdataList("filter");
    }

  },
};
</script>
